package com.shiwaixiangcun.customer.widget

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.*
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import com.shiwaixiangcun.customer.R


/**
 *
 * @author Administrator
 * @date 2017/12/8
 */

class TicketView : ConstraintLayout {


    /**
     * 屏幕像素密度
     */
    private val metrics = resources.displayMetrics
    /**
     * 圆圈的直径
     */
    private var mCircleRadius = (6 * metrics.density).toInt()

    //默认转化成为Dp


    /**
     * 圆圈颜色
     */
    private var mCircleColor = Color.parseColor("#ffffff")

    private var marginBottom = (38 * metrics.density).toInt()

    private var showDivider = false

    private var mCirclePaint: Paint? = null


    /**
     * 设置 圆圈半径
     *
     * @param radius
     */
    fun setCircleRadius(radius: Float) {
        mCircleRadius = (radius * metrics.density).toInt()
        invalidate()
    }

    fun setCircleMarginBottom(distance: Int) {
        marginBottom = (distance * metrics.density).toInt()
        invalidate()

    }

    fun setCirclePaint(circlePaint: Paint) {
        mCirclePaint = circlePaint
    }


    fun setCircleColor(color: Int) {
        mCircleColor = color
        mCirclePaint!!.color = mCircleColor
        invalidate()
    }

    fun setCircleColorRes(res: Int) {
        this.mCircleColor = resources.getColor(res)
        mCirclePaint!!.color = mCircleColor
        invalidate()
    }


    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

        var ta: TypedArray? = if (attrs == null) null else getContext().obtainStyledAttributes(attrs, R.styleable.TicketView)

        if (ta !== null) {
            mCircleColor = ta.getColor(R.styleable.TicketView_circle_color, mCircleColor)
            mCircleRadius = ta.getDimensionPixelSize(R.styleable.TicketView_circle_radius, 6)
            marginBottom = ta.getDimensionPixelOffset(R.styleable.TicketView_circle_margin_bottom, 38)
            showDivider = ta.getBoolean(R.styleable.TicketView_show_divider, false)

        }
        ta!!.recycle()
        mCirclePaint = Paint(Paint.ANTI_ALIAS_FLAG)
        //设置是否使用抗锯齿功能，会消耗较大资源，绘制图形速度会变慢。
        mCirclePaint!!.isDither = true
        //设定是否使用图像抖动处理，会使绘制出来的图片颜色更加平滑和饱满，图像更加清晰
        mCirclePaint!!.color = mCircleColor
        mCirclePaint!!.style = Paint.Style.FILL
    }


    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val width = measuredWidth
        val height = measuredHeight
        if (showDivider) {
            mCirclePaint!!.style = Paint.Style.STROKE
            mCirclePaint!!.color = resources.getColor(R.color.color_divider_0_5)
            val path = Path()
            path.moveTo((0 + 5 + mCircleRadius).toFloat(), (height - marginBottom).toFloat())
            path.lineTo((width - 5 - mCircleRadius).toFloat(), (height - marginBottom).toFloat())
            val effects = DashPathEffect(floatArrayOf(16f, 8f, 16f, 8f), 1f)
            mCirclePaint!!.pathEffect = effects
            canvas.drawPath(path, mCirclePaint)
        }
        mCirclePaint!!.color = mCircleColor
        mCirclePaint!!.style = Paint.Style.FILL
        canvas.drawCircle((0 + 5).toFloat(), (height - marginBottom).toFloat(), mCircleRadius.toFloat(), mCirclePaint!!)
        canvas.drawCircle((width - 5).toFloat(), (height - marginBottom).toFloat(), mCircleRadius.toFloat(), mCirclePaint!!)

    }
}
